package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Business;
import cn.wolfcode.domain.ConsumptionItem;
import cn.wolfcode.enums.ConsumptionStatusEnum;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/consumptionItem")
public class ConsumptionItemController {
    @Autowired
    private IConsumptionItemService  consumptionItemService;
    @Autowired
    private IBusinessService businessService;
    @RequestMapping("/list")
    @RequiredPermission(name="结算单明细查询",expression = "consumptionItem:list")
    public String list(Model model,QueryObject qo){
        model.addAttribute("businesses",businessService.listAll());
        model.addAttribute("consumptionStatusEnums", ConsumptionStatusEnum.values());
        PageInfo<ConsumptionItem> pageInfo = consumptionItemService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "consumptionItem/list";


//                List<ConsumptionItem> consumptionItems = consumptionItemService.listAll();
//        model.addAttribute("consumptionItems",consumptionItems);
//        return "consumptionItem/list";  //找视图 / WEB-INF/views/consumptionItem/list.jsp
    }
    @RequestMapping("/delete")
    @RequiredPermission(name="结算单明细删除",expression = "consumptionItem:delete")
    public String delete(Long id){
        if (id!=null) {
            consumptionItemService.delete(id);
        }
        return "redirect:/consumptionItem/list";  //让浏览器重新发一次请求
    }
    @RequestMapping("/input")
    @RequiredPermission(name="结算单明细新增或修改",expression = "consumptionItem:input")
    public String input(Long id,Model model){
        if (id!=null) {

            ConsumptionItem consumptionItem = consumptionItemService.get(id);
            model.addAttribute("consumptionItem",consumptionItem);
        }
        return "consumptionItem/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="结算单明细新增或修改",expression = "consumptionItem:saveOrUpdate")
    public String saveOrUpdate(ConsumptionItem consumptionItem){
        if (consumptionItem.getId()!=null) {
            consumptionItemService.update(consumptionItem);
        }else {
          consumptionItemService.save(consumptionItem);
        }
        return "redirect:/consumptionItem/list";
    }
}
